<template>
	<view>
		
	
		<uni-forms ref="form" :modelValue="user" :rules="rules" validateTrigger="blur">
			<uni-forms-item label="姓名" name="username">
				<uni-easyinput  type="text" v-model="user.username" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="pwd">
				<uni-easyinput  class="input" v-model="user.pwd" type="password" placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submitForApp">注册</button>
		<button @click="goLogin">直接登录</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				
				user: {
					username: '',
					pwd: ''
				},
				rules: {
					username: {
						rules: [{
								required: true,
								errorMessage: '用户名必填'
							},
							{
								pattern: /^[a-zA-Z]\w{3,7}$/,
								errorMessage: "4-8位字符组成,首字符必须是字母"
							}
						],
						label:'姓名',
					},
					pwd: {
						rules: [{
								required: true,
								errorMessage: '密码必填'
							},
							{
								pattern: /^[a-zA-Z]\w{3,7}$/,
								errorMessage: "4-8位字符组成,首字符必须是字母"
							}
						],
						label:'密码',
					}
				}
				
			}
		},
		methods: {
			submitForApp() {
				this.$refs.form.validate().then(() => {
					this.$request('/user/username/register', {
						method: 'POST',
						data: this.user
					}).then(res => {
						if (res.code === 0) {
							uni.showToast({
								title:"注册成功",
								success: () => {
									uni.redirectTo({
										url: "/pages/login/login"
									})
								}
							})
						}
					})
				}).catch(() => {
					uni.showToast({
						title: "表单校验不通过",
						icon: 'error'
					})
				})
			},
			goLogin(){
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
	}
</script>

<style>

</style>